limsTrainRecord.searchPage = {

    getGridParam: function () {
        var _this = this
        return {
            type: 'grid',
            id: _this.idManager.trainRecordGrid,
            url: limsTrainRecord.baseUrl + '/queries/searchable',
            columns: limsTrainRecord.getSearchListColumns(),
            onLoadSuccess: function (data) {
                if (!Gikam.isEmpty(data)) {
                    this.activeRowByIndex(0)
                } else {
                    _this.refreshTabs(null)
                }
            },
            onRowActive: function (index, row) {
                _this.refreshTabs(row)
            },
            page: true,
        }
    },

    refreshTabs: function (row) {
        let _this = this
        Gikam.getAsyncComp(_this.idManager.auditRecordGrid).done(function (comp) {
            if (row && row.id) {
                comp.refresh({
                    url: IFM_CONTEXT + '/secure/core/module/sys/bpmn-comments/queries?table=T_LIMS_TRAIN_RECORD&id=' + row.id
                })
            } else {
                comp.refresh({
                    url: IFM_CONTEXT + '/secure/core/module/sys/bpmn-comments/queries?table=T_LIMS_TRAIN_RECORD&id=' + 0
                })
            }
        })
        _this.refreshAttachments(_this.idManager.trainRecordGrid)
    },

    getAttachUploaderId: function (gridId) {
        return gridId + '-uploader'
    },

    refreshAttachments: function (gridId) {
        let _this = this
        let grid = Gikam.getComp(gridId)
        let actRow = grid.getActivedRow()
        Gikam.getAsyncComp(_this.getAttachUploaderId(gridId)).done(uploader => {
            if (actRow && actRow.id) {
                uploader.setOptions({
                    bizId: actRow.id,
                })
            } else {
                uploader.setOptions({
                    bizId: -1,
                })
            }
        })
    },

    getAttachmantStruct: function (gridId, dbTable, readonly) {
        let _this = this
        return {
            type: 'uploader',
            immediateUpload: true,
            id: _this.getAttachUploaderId(gridId),
            dbTable: dbTable,
            filter: false,
            genericQuery: false,
            readonly: readonly,
            onBeforeUpload: function () {
                var activedRow = Gikam.getComp(gridId).getActivedRow()
                if (Gikam.isEmpty(activedRow)) {
                    Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM', 200)
                    return false
                }
            },
        }
    },

    getAuditRecordGrid: function () {
        let _this = this
        return {
            type: 'grid',
            id: _this.idManager.auditRecordGrid,
            columns: limsTrainRecord.getFlowListColumns()
        }
    },

    getBaseInfoTab: function () {
        var _this = this
        return {
            type: 'tab',
            panels: [{
                title: 'GIKAM.FILE.ATTACHMENT',
                items: [_this.getAttachmantStruct(_this.idManager.trainRecordGrid, 'T_LIMS_TRAIN_RECORD', true)]
            }, {
                title: 'T_CORE_FILE.OPERATE.OPERATION.RECORD',
                items: [_this.getAuditRecordGrid()]
            }]
        }
    },

    idManager: {
        trainRecordGrid: 'lims-train-record-search-list-grid',
        auditRecordGrid: 'lims-train-record-search-list-grid' + '-record',
    },

    urlManager: {
        trainRecordGrid: limsTrainRecord.baseUrl,
    },

    create: function () {
        var _this = this
        Gikam.create('layout', {
            id: 'lims-train-record-search-list-layout',
            renderTo: workspace.window.$dom,
            north: {
                height: '55%',
                items: [this.getGridParam()]
            },
            center: {
                items: [this.getBaseInfoTab()]
            }
        })
    },

    init: function () {
        this.create()
    }
}
